package com.qingfeng.medical.controller.front.info;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qingfeng.currency.dozer.DozerUtils;
import com.qingfeng.medical.base.controller.BaseController;
import com.qingfeng.medical.base.log.annotation.Log;
import com.qingfeng.medical.base.log.enums.BusinessType;
import com.qingfeng.medical.base.result.Result;
import com.qingfeng.medical.biz.information.domain.dto.PatientSaveDTO;
import com.qingfeng.medical.biz.information.domain.dto.PatientUpdateDTO;
import com.qingfeng.medical.biz.information.domain.entity.Dict;
import com.qingfeng.medical.biz.information.domain.entity.Patient;
import com.qingfeng.medical.biz.information.domain.vo.PatientVo;
import com.qingfeng.medical.biz.information.service.DictService;
import com.qingfeng.medical.biz.information.service.PatientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;


/**
 * 就诊人表
 *
 * @author 清风学Java
 * @date 2023-04-27 16:26:12
 */
@Api(tags = "就诊人表管理")
@RestController
@RequestMapping("/api/info/patient")
@Validated
public class PatientApiController extends BaseController {

    @Autowired
    private PatientService patientService;
    @Autowired
    private DictService dictService;
    @Autowired
    private DozerUtils dozerUtils;

    @ApiOperation(value = "根据用户Id查询就诊人列表", notes = "根据用户Id查询就诊人列表")
    @GetMapping("/list/{id}")
    @Log(title = "前台就诊人管理", businessType = BusinessType.SELECT, message = "根据用户Id查询就诊人列表")
    public Result<List<Patient>> patientListById(@PathVariable("id") Long id) {
        return Result.ok(
                patientService.list(
                        new LambdaQueryWrapper<Patient>()
                                .eq(Patient::getUserId, id)
                )
        );
    }

    @ApiOperation(value = "根据用户Id查询已认证就诊人列表", notes = "根据用户Id查询已认证就诊人列表")
    @GetMapping("/list/status/{id}")
    @Log(title = "前台就诊人管理", businessType = BusinessType.SELECT, message = "根据用户Id查询已认证就诊人列表")
    public Result<List<Patient>> patientListStatusById(@PathVariable("id") Long id) {
        return Result.ok(
                patientService.list(
                        new LambdaQueryWrapper<Patient>()
                                .eq(Patient::getUserId, id)
                                .eq(Patient::getStatus, 1)
                )
        );
    }

    @ApiOperation(value = "根据Id查询就诊人表信息", notes = "根据Id查询就诊人表信息")
    @GetMapping("/info/{id}")
    @Log(title = "前台就诊人管理", businessType = BusinessType.SELECT, message = "根据Id查询就诊人表信息")
    public Result<PatientVo> info(@PathVariable("id") Long id) {
        Patient patient = patientService.getById(id);
        PatientVo patientVo = dozerUtils.map2(patient, PatientVo.class);
        HashMap<String, String> param = new HashMap<>(16);

        param.put("provinceString", dictService.getOne(
                        new LambdaQueryWrapper<Dict>()
                                .eq(Dict::getDictValue, patient.getProvinceCode())
                ).getDictName()
        );
        param.put("cityString", dictService.getOne(
                        new LambdaQueryWrapper<Dict>()
                                .eq(Dict::getDictValue, patient.getCityCode())
                ).getDictName()
        );
        param.put("districtString", dictService.getOne(
                        new LambdaQueryWrapper<Dict>()
                                .eq(Dict::getDictValue, patient.getDistrictCode())
                ).getDictName()
        );

        patientVo.setParam(param);
        return Result.ok(patientVo);
    }


    @ApiOperation(value = "保存就诊人表信息", notes = "保存就诊人表信息")
    @PostMapping("/save")
    @Log(title = "就诊人管理", businessType = BusinessType.INSERT, message = "保存就诊人表信息")
    public Result savePatient(@RequestBody @Validated PatientSaveDTO patientSaveDTO) {
        patientService.savePatient(patientSaveDTO);
        return Result.ok();
    }

    @ApiOperation(value = "根据Id修改就诊人表信息", notes = "根据Id修改就诊人表信息")
    @PutMapping("/update")
    @Log(title = "就诊人管理", businessType = BusinessType.UPDATE, message = "根据Id修改就诊人表信息")
    public Result updatePatient(@RequestBody @Validated PatientUpdateDTO patientUpdateDTO) {
        patientService.updatePatientById(patientUpdateDTO);
        return Result.ok();
    }

    @ApiOperation(value = "删除就诊人表信息", notes = "删除就诊人表信息")
    @DeleteMapping("/delete/{id}")
    @Log(title = "前台就诊人管理", businessType = BusinessType.DELETE, message = "删除就诊人表信息")
    public Result delete(@PathVariable("id") Long id) {
        patientService.removeById(id);
        return Result.ok();
    }
}
